package com.pearadmin.system.timedTask;

import com.alibaba.fastjson.JSON;
import com.pearadmin.common.tools.sequence.SequenceUtil;
import com.pearadmin.system.domain.SysAxisShake;
import com.pearadmin.system.service.ISysAxisService;
import com.pearadmin.system.service.ISysAxisShakeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * redis定时同步到mysql数据库
 */
@Slf4j
@Component //把此类托管给Spring
public class RedisSyncMysql {

    @Resource
    private ISysAxisService sysAxisService;

    @Resource
    private ISysAxisShakeService sysAxisShakeService;

//    @Scheduled(cron = "0 0 1 1/1 * ? ") // cron 表达式，每天1点执行一次
    @Scheduled(cron = "* 0/1 * * * ? ") // cron 表达式，每分钟执行一次
    public void redisSyncMysql(){
        SysAxisShake sysAxisShake=new SysAxisShake();
        //连接redis数据库
        Jedis jedis=new Jedis();
        jedis.auth("root");

        Calendar cal = Calendar.getInstance();
        //获取前3天的日期
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -3);
        Date date= cal.getTime();
        String dateBeforeTime=JSON.toJSONString(date);
        Date date2=new Date();
        String dateNowTime=JSON.toJSONString(date2);

        List<String> axisId=sysAxisService.selectAllAxisCount();
//      查出主键对应的所有value值　zrange key　0 -1
//      zrangebyscore zset的key 起始score 终止score (withscore),默认是包含端点值的
        //遍历主键
        for (String id : axisId){
            //查出所有主键最近3天的所有数据
            Set<String> result = jedis.zrangeByScore(id,dateBeforeTime,dateNowTime);
            for (String str2 : result) {
                Double score=jedis.zscore(id,str2);
                Date doubleDate= new Date();
                doubleDate.setTime(new Double(score).longValue());//java里面应该是按毫秒
                //去除毫秒
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowTime = df.format(doubleDate);
                java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
                System.out.println(buydate);

                sysAxisShake.setParentId(id);
                sysAxisShake.setCreatTime(buydate);
                int flag=sysAxisShakeService.selectShakeByTime(sysAxisShake);
                if (flag<1){
//              Java字符串去除字符段两端的{}标签
                    String phoneIdList =StringUtils.strip(str2,"{}");
                    List<String> shake=Arrays.asList(phoneIdList.split(","));
                    String shakeAList=shake.get(1);
                    String shakeBList=shake.get(2);
                    String axisTypeList=shake.get(0);
                    List<String> lisA=Arrays.asList(shakeAList.split(":"));
                    String shakeA=lisA.get(1);
                    List<String> lisB=Arrays.asList(shakeBList.split(":"));
                    String shakeB=lisB.get(1);
                    List<String> lisType=Arrays.asList(axisTypeList.split(":"));
                    String axisType=lisType.get(1);

//                   BigDecimal a = new BigDecimal(shakeA);
//                    BigDecimal b = new BigDecimal(shakeB);
//                    Double A = a.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//                    Double B = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    //格式里的时如果用hh表示用12小时制，HH表示用24小时制。MM必须是大写!
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                   System.out.println(sdf.format(doubleDate));
                    sysAxisShake.setId(SequenceUtil.makeStringId());
                    sysAxisShake.setShakeA(Double.parseDouble(shakeA));
                    sysAxisShake.setShakeB(Double.parseDouble(shakeB));
                    sysAxisShake.setAxisType(axisType);
                    sysAxisShakeService.insertSysAxisShake(sysAxisShake);

                }

            }

        }

    }

}
